﻿using System;
using System.Data;
using System.Collections.Generic;

namespace WinFormClient
{
    public class ColumnMapCollection
    {
        int _primaryKeyIndex = -1;
        string _primaryKey = null;
        List<ColumnMap> _list = new List<ColumnMap>();

        public void Add(ColumnMap colMap)
        {
            this._list.Add(colMap);
        }

        public void Add(
            string columnName,
            string dbDataType,
            string fieldName,
            string csDataType,
            int dataLength,
            string comments,
            string indexName,
            string defaultValue,
            int visibleIndex,
            string visible,
            string editable,
            string control,
            string spin,
            string propertyName,
            bool notNull)
        {
            if (!String.IsNullOrEmpty(indexName))
            {
                if (String.IsNullOrEmpty(this._primaryKey))
                {
                    this._primaryKey = indexName;
                    this._primaryKeyIndex = this._list.Count;
                }
                else
                {
                    if (indexName == this._primaryKey)
                    {
                        this._primaryKey = null;
                        this._primaryKeyIndex = -1;
                    }
                }
            }
            this._list.Add(new ColumnMap(columnName, dbDataType, fieldName, csDataType, dataLength, comments, indexName, defaultValue, visibleIndex, visible, editable, control, spin, propertyName,notNull));
        }

        public int Count { get { return this._list.Count; } }

        public ColumnMap this[int index]
        {
            get
            {
                return this._list[index];
            }
        }

        public ColumnMap PrimaryKeyColumn
        {
            get
            {
                if (this._primaryKeyIndex == -1) return null;
                return this[this._primaryKeyIndex];
            }
        }

        public void Dispose()
        {
            this._list.Clear();
        }
    }
}
